<?php

class SiteCommand extends CConsoleCommand {

    public $categoryUrls = array(
        'Ремонт телефонов' => array('id' => 1, 'url' => 'http://market.yandex.ru/guru.xml?CMD=-RR=9,0,0,0-VIS=270-CAT_ID=160043-EXC=1-PG=10&hid=91491'),
        'Ремонт планшетов' => array('id' => 2, 'url' => 'http://market.yandex.ru/guru.xml?CMD=-RR=0,0,0,0-VIS=270-CAT_ID=6427101-EXC=1-PG=10&hid=6427100'),
        'Ремонт ноутбуков' => array('id' => 3, 'url' => 'http://market.yandex.ru/guru.xml?CMD=-RR%3D0%2C0%2C0%2C0-VIS%3D8070-CAT_ID%3D432460-EXC%3D1-PG%3D10&hid=91013'),
        'Ремонт фотоаппаратов' => array('id' => 6, 'url' => 'http://market.yandex.ru/guru.xml?CMD=-RR=9,0,0,0-VIS=270-CAT_ID=100514-EXC=1-PG=10&hid=91148'),
        'Ремонт часов' => array('id' => 10, 'url' => 'http://market.yandex.ru/guru.xml?CMD=-RR=0,0,0,0-VIS=270-CAT_ID=2431654-EXC=1-PG=10&hid=91259'),
        'Ремонт телевизоров' => array('id' => 9, 'url' => 'http://market.yandex.ru/guru.xml?CMD=-RR=0,0,0,0-VIS=270-CAT_ID=108206-EXC=1-PG=10&hid=90639'),
        'Ремонт электронных книг' => array('id' => 7, 'url' => 'http://market.yandex.ru/guru.xml?CMD=-RR=0,0,0,0-VIS=270-CAT_ID=2666443-EXC=1-PG=10&hid=2417247'),
        'Ремонт mp3 плееров' => array('id' => 8, 'url' => 'http://market.yandex.ru/guru.xml?CMD=-RR=0,0,0,0-VIS=270-CAT_ID=137555-EXC=1-PG=10&hid=90560'),
    );
    public $categoryTopUrls = array(
        'Ремонт телефонов' => array('id' => 1, 'url' => 'http://market.yandex.ru/guru.xml?CMD=-RR%3D9%2C0%2C0%2C0-VIS%3D8070-CAT_ID%3D160043-EXC%3D1-PG%3D10&hid=91491&ncrnd=5056'),
        'Ремонт планшетов' => array('id' => 2, 'url' => 'http://market.yandex.ru/guru.xml?CMD=-RR%3D0%2C0%2C0%2C0-VIS%3D8070-CAT_ID%3D6427101-EXC%3D1-PG%3D10&hid=6427100'),
        'Ремонт ноутбуков' => array('id' => 3, 'url' => 'http://market.yandex.ru/guru.xml?CMD=-RR%3D0%2C0%2C0%2C0-VIS%3D8070-CAT_ID%3D432460-EXC%3D1-PG%3D10&hid=91013'),
        'Ремонт фотоаппаратов' => array('id' => 6, 'url' => 'http://market.yandex.ru/guru.xml?CMD=-RR%3D9%2C0%2C0%2C0-VIS%3D8070-CAT_ID%3D100514-EXC%3D1-PG%3D10&hid=91148'),
        'Ремонт часов' => array('id' => 10, 'url' => 'http://market.yandex.ru/guru.xml?CMD=-RR%3D0%2C0%2C0%2C0-VIS%3D8070-CAT_ID%3D2431654-EXC%3D1-PG%3D10&hid=91259'),
        'Ремонт телевизоров' => array('id' => 9, 'url' => 'http://market.yandex.ru/guru.xml?CMD=-RR%3D0%2C0%2C0%2C0-VIS%3D8070-CAT_ID%3D108206-EXC%3D1-PG%3D10&hid=90639'),
        'Ремонт электронных книг' => array('id' => 7, 'url' => 'http://market.yandex.ru/guru.xml?CMD=-RR%3D0%2C0%2C0%2C0-VIS%3D8070-CAT_ID%3D2666443-EXC%3D1-PG%3D10&hid=2417247'),
        'Ремонт mp3 плееров' => array('id' => 8, 'url' => 'http://market.yandex.ru/guru.xml?CMD=-RR%3D0%2C0%2C0%2C0-VIS%3D8070-CAT_ID%3D137555-EXC%3D1-PG%3D10&hid=90560'),
    );
    public $urls = array('http://market.yandex.ru/', 'http://market.yandex.ru/catalogmodels.xml?CAT_ID=140982&hid=90633', 'http://market.yandex.ru/product/8471273/reviews?hid=90633', 'http://market.yandex.ru/user/prime-computers/reviews', 'http://market.yandex.ru/catalog?hid=7877999');
    public $errors = array();
    public $config = array(
        'ooospares' => 'http://ooospares.ru/search.html?c=search&pgsize=50&searche=1&idc=0&stype=1&searchword=',
        'gsmbaza' => 'http://www.gsmbaza.ru/search.php?x=9&y=16&stype=name&search=',
        'green_spark' => 'green-spark.ru/catalog',
    );
    public $date;
    
    public function actionTest() {
        shell_exec('/root/telegram/tg/bin/telegram-cli -k /root/telegram/tg/server.pub -WR -e "msg toxir lalala2"');
    }

    public function actionExecute() {
        $pid = pcntl_fork();
        if ($pid == -1) {
            die("could not fork");
        } else if ($pid) {
            // это родитель
        } else {
            $this->actionLoop();
        }
    }

    public function actionLoop() {
        $old_minute = -1;
        do {
            $cur_minute = date("i");
            if ($cur_minute != $old_minute)
                $this->actionTelegram();
            if (date("H:i") == '22:00')
                $this->actionTelegramDaily();
            $old_minute = $cur_minute;
            usleep(50000);
        } while (true);
    }

    private function _SQL1() {
        $sql = '';
        $path = '/var/www/telegram-history/';
        $ids1 = file_exists($path . 'msk-' . $this->date . '.txt') ? file_get_contents($path . 'msk-' . $this->date . '.txt') : false;
        if ($ids1 && !empty($ids1) && $ids1 != '[]') {
            $sql = ' AND f.OId NOT IN(' . implode(',', json_decode($ids1, true)) . ')';
        }

        return 'SELECT                    
                    f.OId,
                    CONVERT(
                        DATE ,
                        f.CreatedDatetime ,
                        23
                    ) AS create_date_formatted ,
                    o.Name AS sc_name ,
                    v.Name AS action_name ,
                    f.Primechanie353972206000 AS primichanie ,
                    f."Gorod484353666000" AS city ,
                    f.Prihod353972212000 AS prihod ,
                    f.Rashod353972214000 AS rasxod ,
                    f.Ostatok866750364000 AS ostatok ,
                    f.CreatedBy AS created_by
                FROM
                    tblDvizhenie_deneg_CENTR353972147000 f LEFT JOIN tblVid_platezha9028492000 v
                        ON v.OId = f.Vid_platezha353972176000 LEFT JOIN tblOrganisationStructure o
                        ON o.OId = f.SC353972222000
                WHERE
                    CONVERT(
                        DATE ,
                        f.CreatedDatetime ,
                        23
                    ) = \'' . $this->date . '\'
                    AND f."Gorod484353666000" in (\'MSK\',\'SPB\')' . $sql . '

                ORDER BY
                    f.CreatedDatetime DESC';
    }

    private function _SQL2() {
        $sql = '';
        $path = '/var/www/telegram-history/';

        $ids1 = file_exists($path . 'other-' . $this->date . '.txt') ? file_get_contents($path . 'other-' . $this->date . '.txt') : false;
        if ($ids1 && !empty($ids1) && $ids1 != '[]') {
            $sql = ' AND f.OId NOT IN(' . implode(',', json_decode($ids1, true)) . ')';
        }
        return 'SELECT                    
                    f.OId,
                    CONVERT(
                        DATE ,
                        f.CreatedDatetime ,
                        23
                    ) AS create_date_formatted ,
                    o.Name AS sc_name ,
                    v.Name AS action_name ,
                    f.Primechanie9028320000 AS primichanie ,
                    LEFT( o.Name ,3 ) AS city ,
                    f.Prihod9027279000 AS prihod ,
                    f.Rashod9027451000 AS rasxod ,
                    f.Saldo1097460112000 AS ostatok ,
                    f.CreatedBy AS created_by
                FROM
                    tblDvizhenie_deneg9025708000 f LEFT JOIN tblVid_platezha9028492000 v
                        ON v.OId = f.Vid_platezha9029892000 LEFT JOIN tblOrganisationStructure o
                        ON o.OId = f.SC274147102000
                WHERE  CONVERT(
                        DATE ,
                        f.CreatedDatetime ,
                        23
                    ) = \'' . $this->date . '\' AND LEFT( o.Name ,3 ) not in (\'MSK\',\'SPB\')' . $sql . '
                ORDER BY
                    f.CreatedDatetime desc';
    }

    public function number_format($num) {
        return number_format($num, 0, '.', '\'');
    }

    public function actionTelegram() {
        //$cmdExample = '~/tg/bin/telegram-cli -k ~/tg/server.pub -WR -e "msg toxir rfgdfgdfgdfg"';
//        exec('~/tg/bin/telegram-cli -k ~/tg/server.pub -WR -e "send_text shef /var/www/test.txt"');exit;
        $date = $this->date = date('Y-m-d');
        $path = '/var/www/telegram-history/';
        $file_msk_spb = $date . '-report-msk-spb.txt';
        $file_other = $date . '-report-other.txt';
        $ids_msk_spb = array();
        $ids_other = array();
        $txt_msk_spb = '';
        $txt_other = '';
//        file_put_contents($path . 'msk-' . $this->date . '.txt', '');
//        file_put_contents($path . 'other-' . $this->date . '.txt', '');
        $sql1 = $this->_SQL1();
        $sql2 = $this->_SQL2();

        $result_msk_spb = Yii::app()->dbClaris->getCommandBuilder()->createSqlCommand($sql1)->queryAll();

        $result_Other = Yii::app()->dbClaris->getCommandBuilder()->createSqlCommand($sql2)->queryAll();
//        print_r($result_msk_spb);exit;
        foreach ($result_msk_spb as $sc_msk) {
            $sc_msk['sc_name'] = iconv("cp1251", "UTF-8", $sc_msk['sc_name']);
            $sc_msk['action_name'] = iconv("cp1251", "UTF-8", $sc_msk['action_name']);
            if (empty($sc_msk['sc_name']))
                continue;
            $ids_msk_spb[] = $sc_msk['OId'];
            if ($sc_msk['rasxod'] > 0 || $sc_msk['prihod'] > 0)
                $txt_msk_spb .= ($sc_msk['rasxod'] ? 'Расход ' : 'Приход ') . ': ' . $sc_msk['sc_name'] . ', ' . $sc_msk['action_name'] . ' - ' . $this->number_format($sc_msk['rasxod'] ? $sc_msk['rasxod'] : $sc_msk['prihod']) . "\n";
        }

        foreach ($result_Other as $sc) {
            $sc['sc_name'] = iconv("cp1251", "UTF-8", $sc['sc_name']);
            $sc['action_name'] = iconv("cp1251", "UTF-8", $sc['action_name']);
            if (empty($sc['sc_name']))
                continue;
            $ids_other[] = $sc['OId'];
            if ($sc['rasxod'] > 0 || $sc['prihod'] > 0)
                $txt_other .= ($sc['rasxod'] ? 'Расход ' : 'Приход ') . ': ' . $sc['sc_name'] . ', ' . $sc['action_name'] . ' - ' . $this->number_format($sc['rasxod'] ? $sc['rasxod'] : $sc['prihod']) . "\n";
        }

        $txt_msk_spb = mb_convert_encoding($txt_msk_spb, 'UTF-8', 'auto');
        $txt_other = mb_convert_encoding($txt_other, 'UTF-8', 'auto');

        file_put_contents($path . $file_msk_spb, $txt_msk_spb);
        file_put_contents($path . $file_other, $txt_other);

        $ids1 = file_exists($path . 'msk-' . $this->date . '.txt') ? json_decode(file_get_contents($path . 'msk-' . $this->date . '.txt'), true) : false;
        $ids2 = file_exists($path . 'other-' . $this->date . '.txt') ? json_decode(file_get_contents($path . 'other-' . $this->date . '.txt'), true) : false;

        if (!$ids1) {
            file_put_contents($path . 'msk-' . $this->date . '.txt', json_encode($ids_msk_spb));
        } else {
            $ids1 = array_merge($ids_msk_spb, $ids1);
            file_put_contents($path . 'msk-' . $this->date . '.txt', json_encode($ids1));
        }
        if (!$ids2) {
            file_put_contents($path . 'other-' . $this->date . '.txt', json_encode($ids_other));
        } else {
            $ids2 = array_merge($ids_other, $ids2);
            file_put_contents($path . 'other-' . $this->date . '.txt', json_encode($ids2));
        }

        chmod($path . $file_msk_spb, 0666);
        chmod($path . $file_other, 0666);
        chmod($path . 'msk-' . $this->date . '.txt', 0666);
        chmod($path . 'other-' . $this->date . '.txt', 0666);

        shell_exec('/root/telegram/tg/bin/telegram-cli -k /root/telegram/tg/server.pub -WR -e "send_text shef ' . $path . $file_msk_spb . '"');
        shell_exec('/root/telegram/tg/bin/telegram-cli -k /root/telegram/tg/server.pub -WR -e "send_text shef ' . $path . $file_other . '"');
        
        shell_exec('/root/telegram/tg/bin/telegram-cli -k /root/telegram/tg/server.pub -WR -e "send_text Miraziz ' . $path . $file_msk_spb . '"');
        shell_exec('/root/telegram/tg/bin/telegram-cli -k /root/telegram/tg/server.pub -WR -e "send_text Miraziz ' . $path . $file_other . '"');
    }

    private function DailyMSK_SPBSQL($city = 'MSK') {
        return 'select t.action_name, sum(t.prihod) as prihod, sum(t.rasxod) as rasxod
                    from (
                    SELECT
                        f.OId ,
                        CONVERT(
                            DATE ,
                            f.CreatedDatetime ,
                            23
                        ) AS create_date_formatted ,
                        o.Name AS sc_name ,
                        v.Name AS action_name ,
                        f.Primechanie353972206000 AS primichanie ,
                        f."Gorod484353666000" AS city ,
                        f.Prihod353972212000 AS prihod ,
                        f.Rashod353972214000 AS rasxod ,
                        f.Ostatok866750364000 AS ostatok ,
                        f.CreatedBy AS created_by
                    FROM
                        tblDvizhenie_deneg_CENTR353972147000 f LEFT JOIN tblVid_platezha9028492000 v
                            ON v.OId = f.Vid_platezha353972176000 LEFT JOIN tblOrganisationStructure o
                            ON o.OId = f.SC353972222000
                    WHERE
                        CONVERT(
                            DATE ,
                            f.CreatedDatetime ,
                            23
                        ) = \'' . $this->date . '\'
                        AND f."Gorod484353666000" IN(
                            \'' . $city . '\' 
                        )
                     ) t 
                    group by t.action_name
                    ';
    }

    private function DailyOtherSCSQL($city = 'EKB') {
        return 'select t.action_name, sum(t.prihod) as prihod, sum(t.rasxod) as rasxod from 
                    (SELECT                        
                        f.OId ,
                        CONVERT(
                            DATE ,
                            f.CreatedDatetime ,
                            23
                        ) AS create_date_formatted ,
                        o.Name AS sc_name ,
                        v.Name AS action_name ,
                        f.Primechanie9028320000 AS primichanie ,
                        LEFT( o.Name ,3 ) AS city ,
                        f.Prihod9027279000 AS prihod ,
                        f.Rashod9027451000 AS rasxod ,
                        f.Saldo1097460112000 AS ostatok ,
                        f.CreatedBy AS created_by
                    FROM
                        tblDvizhenie_deneg9025708000 f LEFT JOIN tblVid_platezha9028492000 v
                            ON v.OId = f.Vid_platezha9029892000 LEFT JOIN tblOrganisationStructure o
                            ON o.OId = f.SC274147102000
                    WHERE
                        CONVERT(
                            DATE ,
                            f.CreatedDatetime ,
                            23
                        ) = \'' . $this->date . '\'
                        AND ltrim(rtrim(LEFT( o.Name ,3 ))) IN(
                            \'' . $city . '\' 
                        )

                    ) t 
                    group by t.action_name';
    }

    public function actionTelegramDaily() {
        $date = $this->date = date('Y-m-d');
        $path = '/var/www/telegram-history/daily/';
        $file_msk_spb = $date . '-report-msk-spb.txt';
        $file_other = $date . '-report-other.txt';
        $cities1 = array('MSK' => 'Москва', 'SPB' => 'Санкт-Петербург');
        $cities2 = array('NSK' => 'Новосибирск', 'EKB' => 'Екатеринбург', 'NN' => 'Нижний Новгород');
        $uppercaseCity = array('Москва' => 'МОСКВА', 'Санкт-Петербург' => 'САНКТ-ПЕТЕРБУРГ', 'Новосибирск' => 'НОВОСИБИРСК', 'Екатеринбург' => 'ЕКАТЕРИНБУРГ', 'Нижний Новгород' => 'НИЖНИЙ НОВГОРОД');
        foreach ($cities1 as $city => $cityName) {
            $txt_m = '';
            $prixod = 0;
            $prasxod = 0;
            $rasxod_vtch = '';
            $sql1 = $this->DailyMSK_SPBSQL($city);
            $result_msk_spb = Yii::app()->dbClaris->getCommandBuilder()->createSqlCommand($sql1)->queryAll();
            foreach ($result_msk_spb as $sc_m) {
                $prixod += $sc_m['prihod'];
                $prasxod += $sc_m['rasxod'];
                if (!empty($sc_m['rasxod']) && $sc_m['rasxod'] != 0) {
                    $sc_m['action_name'] = iconv("cp1251", "UTF-8", $sc_m['action_name']);
                    $rasxod_vtch .= (empty($sc_m['action_name']) ? 'Не указан' : $sc_m['action_name']) . ' - ' . $this->number_format($sc_m['rasxod']) . "\n";
                }
            }
            $txt_m .= "\n" . '----------------------' . "\n" . $uppercaseCity[$cityName] . ':' . "\n\n" . ' ДОХОДЫ: ' . $this->number_format($prixod) . "\n" . ' РАСХОДЫ: ' . $this->number_format($prasxod) . "\n" . 'в т.ч.:' . "\n" . $rasxod_vtch . "\n";
            $txt_m = mb_convert_encoding($txt_m, 'UTF-8', 'auto');
            $otherTXT = $this->actionTelegramOther(array($city => $cityName));
            file_put_contents($path . $file_msk_spb, $txt_m . $otherTXT, FILE_APPEND);
        }

        foreach ($cities2 as $city => $cityName) {
            $txt_m = '';
            $prixod = 0;
            $prasxod = 0;
            $rasxod_vtch = '';
            $sql1 = $this->DailyOtherSCSQL($city);
            $result_Other = Yii::app()->dbClaris->getCommandBuilder()->createSqlCommand($sql1)->queryAll();
            foreach ($result_Other as $sc_m) {
                $prixod += $sc_m['prihod'];
                $prasxod += $sc_m['rasxod'];
                if (!empty($sc_m['rasxod']) && $sc_m['rasxod'] != 0)
                    $rasxod_vtch .= (empty($sc_m['action_name']) ? 'Не указан' : $this->number_format($sc_m['action_name'])) . ' - ' . $this->number_format($sc_m['rasxod']) . "\n";
            }
            $txt_m .= "\n" . '----------------------' . "\n" . $uppercaseCity[$cityName] . ':' . "\n\n" . ' ДОХОДЫ: ' . $this->number_format($prixod) . "\n" . ' РАСХОДЫ: ' . $this->number_format($prasxod) . "\n" . (!empty($rasxod_vtch) ? ('в т.ч.:' . "\n" . $rasxod_vtch ) : '') . "\n";
            $txt_m = mb_convert_encoding($txt_m, 'UTF-8', 'auto');
            $otherTXT = $this->actionTelegramOther(array($city => $cityName));
            file_put_contents($path . $file_other, $txt_m . $otherTXT, FILE_APPEND);
        }
        file_put_contents($path . $file_other, "\n" . $this->isxod_vhod(), FILE_APPEND);
        chmod($path . $file_msk_spb, 0666);
        chmod($path . $file_other, 0666);
        
        shell_exec('/root/telegram/tg/bin/telegram-cli -k /root/telegram/tg/server.pub -WR -e "send_text shef ' . $path . $file_msk_spb . '"');
        shell_exec('/root/telegram/tg/bin/telegram-cli -k /root/telegram/tg/server.pub -WR -e "send_text shef ' . $path . $file_other . '"');
        
        shell_exec('/root/telegram/tg/bin/telegram-cli -k /root/telegram/tg/server.pub -WR -e "send_text Miraziz ' . $path . $file_msk_spb . '"');
        shell_exec('/root/telegram/tg/bin/telegram-cli -k /root/telegram/tg/server.pub -WR -e "send_text Miraziz ' . $path . $file_other . '"');
    }

    private function _SQLPV($city) {
        return 'select sum(prinyat) as prinyat,sum(vidan) as vidan from 
                    (
                    select count(1) as vidan, 0 as prinyat from tblZajavki_Desk4233325000 z
                    left join tblOrganisationStructure s on s.OId = z.SC221514532000
                    where z.Status4241906000 = 221651756000 and CONVERT(
                            DATE ,
                            z.Data_vydachi221624922000 ,
                            23
                        ) = \'' . $this->date . '\' and ltrim(rtrim(LEFT(s.Name,3)))=\'' . $city . '\'
                    union 
                    select 0,count(1) from tblZajavki_Desk4233325000 z
                    left join tblOrganisationStructure s on s.OId = z.SC221514532000
                    where CONVERT(DATE ,z.CreatedDatetime,23) = \'' . $this->date . '\' and ltrim(rtrim(LEFT(s.Name,3)))=\'' . $city . '\'
                    ) t';
    }

    private function _SQL_ONLINE_ZAYAVKI($city) {
        $sql = '';
        if ($city == 'Москва') {
            $sql = " IN('Москва', 'Moscow', 'Москва и МО', '1')";
        } else {
            $sql = ' = ' . "'$city'";
        }
        return 'select count(1) as cnt from tblZajavki_onlajn630634238000 where convert(date,CreatedDatetime,23)=\'' . $this->date . '\' and ("Gorod643534976000"' . $sql . ')';
    }

    private function _SQL_ISXOD_VHOD_Z() {
        return "select sum(vxod) vxod,sum(isxod)isxod from(
                    select count(1) as vxod, 0 as isxod from tblZhurnal_zvonkov281581380000 where Napravlenie574850609000='IN' and convert(date,Createddatetime,23)='" . $this->date . "'
                    union 
                    select 0, count(1)from tblZhurnal_zvonkov281581380000 where Napravlenie574850609000='OUT'and convert(date,Createddatetime,23)='" . $this->date . "'
                    )t";
    }

    private function _SQL_OBR_Z() {
        return "select count(1) as cnt from tblObratnye_zvonki646965388000 where convert(date,CreatedDatetime,23)='" . $this->date . "'";
    }

    public function actionTelegramOther(array $cities1) {
        $date = $this->date = date('Y-m-d');
        $path = '/var/www/telegram-history/daily/';
        $file_PV = $date . '-PV.txt';
        $file_msk_spb = $date . '-report-msk-spb.txt';
        $file_other = $date . '-report-other.txt';
        $ISXOD_VHOD_Z = '';
        $OBR_Z = '';
        $txt = '';
        //$cities1 = array('MSK' => 'Москва', 'SPB' => 'Санкт-Петербург', 'NSK' => 'Новосибирск', 'EKB' => 'Екатеринбург', 'NN' => 'Нижний Новгород');
        foreach ($cities1 as $city => $cityName) {
            $PV_text = '';
            $ONLINE_Z = '';
            $sql = $this->_SQLPV($city);
            $result_PV = Yii::app()->dbClaris->getCommandBuilder()->createSqlCommand($sql)->queryRow();
            //$txt .= '-------------------' . "\n" . $cityName . ':' . "\n";
            $PV_text .= 'Принято АПП: ' . $this->number_format($result_PV['prinyat']) . "\n";
            $PV_text .= 'Выдано АПП: ' . $this->number_format($result_PV['vidan']) . "\n";
            $txt .= $PV_text;
            $sql22 = $this->_SQL_ONLINE_ZAYAVKI($cityName);
            $sql22 = iconv("UTF-8", "cp1251", $sql22);
            $result_ONLINE_Z = Yii::app()->dbClaris->getCommandBuilder()->createSqlCommand($sql22)->queryRow();
            $ONLINE_Z = 'Онлайн заявки: ' . $this->number_format($result_ONLINE_Z['cnt']) . "\n";
            $txt .= $ONLINE_Z;
        }
        $txt = mb_convert_encoding($txt, 'UTF-8', 'auto');
        return $txt;
    }

    public function isxod_vhod() {
        $ISXOD_VHOD_Z = '';
        $OBR_Z = '';
        $txt = '';
        $sql = $this->_SQL_ISXOD_VHOD_Z();
        $result_ISXOD_VHOD_Z = Yii::app()->dbClaris->getCommandBuilder()->createSqlCommand($sql)->queryRow();
        $ISXOD_VHOD_Z .= 'Входящие звонки: ' . $this->number_format($result_ISXOD_VHOD_Z['vxod']) . "\n";
        $ISXOD_VHOD_Z .= 'Исходящие звонки: ' . $this->number_format($result_ISXOD_VHOD_Z['isxod']) . "\n";

        $sql = $this->_SQL_OBR_Z();
        $result_SQL_OBR_Z = Yii::app()->dbClaris->getCommandBuilder()->createSqlCommand($sql)->queryRow();
        $OBR_Z = 'Обратные звонки: ' . $this->number_format($result_SQL_OBR_Z['cnt']);
        $txt .= "\n";
        $txt .= $ISXOD_VHOD_Z;
        $txt .= $OBR_Z;
        return $txt;
    }

    public function actionGetSql($fileName = 'mp3.xls') {
        spl_autoload_unregister(array('YiiBase', 'autoload'));
        $phpExcelPath = Yii::getPathOfAlias('ext.phpexcel');
        include($phpExcelPath . DIRECTORY_SEPARATOR . 'PHPExcel.php');
        spl_autoload_register(array('YiiBase', 'autoload'));
        $file = Yii::app()->basePath . '/../uploads/' . $fileName;
        $excel = PHPExcel_IOFactory::load($file);
        $sheets = $excel->getAllSheets();
        foreach ($sheets as $sheet) {
            $services[] = $sheet->toArray();
        }
        $sql = 'SELECT "pageId" FROM mb_pages WHERE  "pageId" > 90013 AND "pageUrl" LIKE \'%mp3-pleeery%\' ORDER BY "pageId"';
        $models = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->queryAll();
        foreach ($models as $model) {
            foreach ($services[0] as $service) {
                file_put_contents('newLink', 'select "newLink"(' . $model['pageId'] . '::int8, ' . $service[0] . '::int2, \'' . $service[2] . '\'::varchar, 0::int2, 0::int2);' . "\r\n", FILE_APPEND);
            }
        }
    }

    public function actionGetYandexID() {
        set_time_limit(0);
        Yii::import('ext.phpQuery.phpQuery');
        $url = 'http://market.yandex.ru/search.xml?hid=90560&text=';
        $sql = 'SELECT
                    m."pageId" AS modelid ,
                    b."pageTitle" AS brand ,
                    m."pageTitle" AS model ,
                    m."pageUrl" AS url ,
                    m."pageYandexId" AS yandexID
                FROM
                    mb_pages m LEFT JOIN mb_pages b
                        ON b."pageId" = m."pageParent"
                WHERE
                    m."pageYandexId" IS NULL
                    AND m."pageUrl" LIKE \'%mp3-pleeery%\'';
        $models = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->queryAll();
//        $models = array_slice($models, 213);
        foreach ($models as $key => $model) {
            $output = Yii::app()->YandexData->get($url . $model['brand'] . ' ' . $model['model']);
            $document = phpQuery::newDocument($output);
            $href = pq($document['a.b-offers__name:first'])->attr('href');
            preg_match('@(.*)modelid=(\d+)(.*)@', $href, $content);
            if (isset($content[2])) {
                $sql = 'UPDATE "mb_pages" SET "pageYandexId"=\'' . $content[2] . '\' WHERE "pageId" = \'' . (int) $model['modelid'] . '\';' . "\r\n";
                file_put_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'updateYaID.txt', $sql, FILE_APPEND | LOCK_EX);
                echo $key . ' - ' . $model['brand'] . ' ' . $model['model'] . ' - ' . $content[2] . "\r\n";
            }
        }
    }

    public function actionGetAllPriceFromYandex2() {
        set_time_limit(0);
        Yii::import('ext.phpQuery.phpQuery');
        $file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'allModels.txt';
        echo '222';
        $handle = fopen($file, "r");
        $content = fread($handle, filesize($file));
        fclose($handle);

//        $output = Yii::app()->YandexData->get('http://market.yandex.ru/guru.xml?CMD=-RR%3D0%2C0%2C0%2C0-VIS%3D8070-CAT_ID%3D432460-EXC%3D1-PG%3D10&hid=91013');   
//        file_put_contents($file, $output);
//        $document = phpQuery::newDocument($output);
//        $pager = pq($document['a.b-pager__next'])->attr('href');
//        if (!empty($pager)) {
//            $nextPage = '';
//            $flag = false;
//            $nextPage = pq($document['a.b-pager__next'])->attr('href');
//            while (!$flag) {
//                $data = file_get_contents($file);
//                sleep(1);
//                $out = Yii::app()->YandexData->get('http://market.yandex.ru/' . $nextPage);
//                $d = phpQuery::newDocument($out);
//                $nextPage = pq($d['.b-pager span .b-pager__next'])->attr('href');
//                if (empty($nextPage))
//                    $flag = true;
//                file_put_contents($file, $data . $out);
//            }
//        }
//        $allContent = file_get_contents($file);
//        $doc = phpQuery::newDocument($allContent);
//
//        foreach ($doc['.page__b-offers__guru .b-offers'] as $popular => $id) {
//            preg_match('@(.*)modelid=(\d+)(.*)@', pq($id)->find('.b-offers__pict a')->attr('href'), $content);
//            $price = pq($id)->find('.b-prices__num')->text();
//            $price = preg_replace("/\D/", '', $price);
//
//            if (isset($content[2])) {
//                $yaID = (int) $content[2];
//                $price = empty($price) || $price == 0 ? -1 : $price;
//                echo $yaID . '-' . $price . "\r\n";
//                $models[] = array('yaID' => $yaID, 'price' => $price);
//            }
//        }
//         file_put_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'tt2.txt', var_export($models, true));
//        var_export($models);
//        exit;
//        print_r($models);exit;
//         $sql = '';
//        foreach ($models as $key => $model) {
//            $sql .= 'UPDATE "mb_pages" SET "pageYandexPrice"=\'' . $model['price'] . '\' WHERE "pageYandexId" = \'' . (int) $model['yaID'] . '\';' . "\r\n";
////            Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->execute();
//            echo $key . ' - ' . $sql . "\r\n";
//        }
//        file_put_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'sql22.txt', $sql);
    }

    public function createSQL($yaid, $price) {
        $sql = 'UPDATE "mb_pages" SET "pageYandexPrice"=\'' . $price . '\' WHERE "pageYandexId" = \'' . (int) $yaid . '\';' . "\r\n";
        file_put_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'sql.txt', $sql, FILE_APPEND | LOCK_EX);
    }

    public function getYaIDAndPrice($allContent) {
        ini_set('memory_limit', '5136M');
        set_time_limit(0);
        Yii::import('ext.phpQuery.phpQuery');
        $doc = phpQuery::newDocument($allContent);
        foreach ($doc['.page__b-offers__guru .b-offers'] as $popular => $id) {
            preg_match('@(.*)modelid=(\d+)(.*)@', pq($id)->find('.b-offers__pict a')->attr('href'), $content);
            $price = pq($id)->find('.b-prices__num')->text();
            $price = preg_replace("/\D/", '', $price);

            if (isset($content[2])) {
                $yaID = (int) $content[2];
                $price = empty($price) || $price == 0 ? -1 : $price;
//                echo $yaID . '-' . $price . "\r\n";
                $models[] = array('yaID' => $yaID, 'price' => $price);
            }
        }
        return $models;
    }

    public function actionGetAllPriceFromYandex() {
        ini_set('memory_limit', '5136M');
        set_time_limit(0);
        Yii::import('ext.phpQuery.phpQuery');
        $file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'allModels.txt';
        $output = Yii::app()->YandexData->get('http://market.yandex.ru/guru.xml?CMD=-RR%3D0%2C0%2C0%2C0-VIS%3D8070-CAT_ID%3D2431654-EXC%3D1-PG%3D10&hid=91259');
        $ms = $this->getYaIDAndPrice($output);
        foreach ($ms as $m) {
            $this->createSQL($m['yaID'], $m['price']);
        }

//        file_put_contents($file, $output);
        $document = phpQuery::newDocument($output);
        $pager = pq($document['a.b-pager__next'])->attr('href');
        if (!empty($pager)) {
            $nextPage = '';
            $flag = false;
            $nextPage = pq($document['a.b-pager__next'])->attr('href');
            while (!$flag) {
                $data = file_get_contents($file);
//                sleep(1);
                $out = Yii::app()->YandexData->get('http://market.yandex.ru/' . $nextPage);
                $ms = $this->getYaIDAndPrice($out);
                foreach ($ms as $m) {
                    $this->createSQL($m['yaID'], $m['price']);
                }
                $d = phpQuery::newDocument($out);
                $nextPage = pq($d['.b-pager span .b-pager__next'])->attr('href');
                if (empty($nextPage))
                    $flag = true;
//                file_put_contents($file, $data . $out);
            }
        }
        exit;
        $allContent = file_get_contents($file);
        $doc = phpQuery::newDocument($allContent);
        echo '1';
        foreach ($doc['.page__b-offers__guru .b-offers'] as $popular => $id) {
            preg_match('@(.*)modelid=(\d+)(.*)@', pq($id)->find('.b-offers__pict a')->attr('href'), $content);
            $price = pq($id)->find('.b-prices__num')->text();
            $price = preg_replace("/\D/", '', $price);

            if (isset($content[2])) {
                $yaID = (int) $content[2];
                $price = empty($price) || $price == 0 ? -1 : $price;
//                echo $yaID . '-' . $price . "\r\n";
                $models[] = array('yaID' => $yaID, 'price' => $price);
            }
        }
//        file_put_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'tt.txt', var_export($models, true));
//        var_export($models);
//        exit;
//        print_r($models);exit;
        $sql = '';
        foreach ($models as $key => $model) {
            $sql .= 'UPDATE "mb_pages" SET "pageYandexPrice"=\'' . $model['price'] . '\' WHERE "pageYandexId" = \'' . (int) $model['yaID'] . '\';' . "\r\n";
//            Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->execute();
//            echo $key . ' - ' . $sql . "\r\n";
        }
        file_put_contents(dirname(__FILE__) . DIRECTORY_SEPARATOR . 'sql.txt', $sql);
    }

    public function actionGetPriceFromYandex() {

        set_time_limit(0);
        Yii::import('ext.phpQuery.phpQuery');
        $c = 0;
        while (true) {
            $sql = 'SELECT "pageYandexPrice", "pageYandexId", "pageId" FROM "mb_pages" WHERE "pageUrl" NOT LIKE \'%remont-chasov%\' and "pageYandexPrice" = 0 AND "pageType" = \'model\' AND "pageYandexId" IS NOT NULL ORDER BY "pageYandexId" DESC';
            $model = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->queryRow();
            if (empty($model)) {
                echo 'Finish';
                exit();
            }
//            print_r($model);
//            echo 'sd';
//            exit;
            if (empty($model['pageYandexId']))
                continue;
            $out = Yii::app()->curl->get('http://market.yandex.ru/model.xml?modelid=' . $model['pageYandexId']);
            $doc = phpQuery::newDocument($out);
            $price = pq($doc['.price__int:first'])->text();
            $price = preg_replace("/\D/", '', $price);
            $price = empty($price) || $price == 0 ? -1 : $price;
            $sql = 'UPDATE "mb_pages" SET "pageYandexPrice"=\'' . $price . '\' WHERE "pageId" = ' . $model['pageId'];
            Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->execute();
            $c++;
            echo $c . ' - ' . $model['pageId'] . ' - ' . $price . "\r\n";
        }
    }

    public function actionHeaders() {
        echo $json = Yii::app()->curl->get('http://mobi03.ru/spares', array('test' => '231231'));

        print_r(json_decode($json, true));
    }

    public function green_spark($search) {
        $products['site'] = __FUNCTION__;
        $regions = array('moskva', 'spb', 'nnov', 'kazan');
        foreach ($regions as $region) {
//            echo 'http://' . $region . '.' . $this->config[__FUNCTION__] . '<br />';
            $out = Yii::app()->curl->post(
                    'http://kazan.green-spark.ru/catalog/page/6/order/up/by/views', array(
                'action' => 'get_list',
                'filter[amount]' => false,
                'filter[original]' => true,
                'filter[search]' => $search,
                'filter[sort]' => false,
                'id_parent' => 0
                    )
            );
            Yii::import('ext.phpQuery');
            print($out);
            $doc = phpQuery::newDocument($out);
            foreach ($doc['.smallparams2'] as $tr) {
                preg_match('#Оптовая цена: (?P<digit>\d+)#', pq($tr)->find('.price')->text(), $matches);
                $products[$region][] = array(
                    'number' => pq($tr)->find('.smalltop b i')->text(),
                    'name' => pq($tr)->find('.cl b a')->text(),
                    'price' => isset($matches['digit']) ? $matches['digit'] : 0,
                    'quantity' => pq($tr)->find('div:nth-child(7) img')->attr('title')
                );
            }
        }
        return $products;
    }

    public function ooospares($search) {
        $out = Yii::app()->curl->get($this->config[__FUNCTION__] . $search);
        Yii::import('ext.phpQuery');
        $doc = phpQuery::newDocument($out);
//echo pq($doc['table[cellspacing="3"]'])->html();
        foreach ($doc['tr[align="center"]'] as $tr) {
            echo pq($tr)->find('td')->text(), '<br>';
        }
    }

// пример простейшего callback'а. практически dummy-func.
    function my_callback($url, $content, $curl_status, $ch) {

        echo 'Скачивание страницы' . $url;
        if (!$curl_status) {
            echo "было успешным. текст страницы:\n$content\n";
        } else {
            echo "выполнилось с ошибкой #$curl_status: " . curl_error($ch) . "\n";
        }
    }

    function http_load($urls, $callback = false) {
        $mh = curl_multi_init();


        $chs = array();
        foreach ($urls as $url) {
            $chs[] = ( $ch = curl_init() );

            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HEADER, 0);
// CURLOPT_RETURNTRANSFER - возвращать значение как результат функции, а не выводить в stdout

            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_multi_add_handle($mh, $ch);
        }

// если $callback задан как false, то функция должна не вызывать $callback, а выдать страницы как результат работы

        if ($callback === false) {
            $results = array();
        }

        $prev_running = $running = null;

        do {
            curl_multi_exec($mh, $running);

            if ($running != $prev_running) {
// получаю информацию о текущих соединениях

                $info = curl_multi_info_read($mh);

                if (is_array($info) && ( $ch = $info['handle'] )) {

// получаю содержимое загруженной страницы
                    $content = curl_multi_getcontent($ch);

// скаченная ссылка
                    $url = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);


                    if ($callback !== false) {
// вызов callback-обработчика
                        $callback($url, $content, $info['result'], $ch);
                    } else {
// добавление в хеш результатов
                        $results[$url] = array('content' => $content, 'status' => $info['result'], 'status_text' => curl_error($ch));
                    }
                }

// обновляю кешируемое число текущих активных соединений
                $prev_running = $running;
            }
        } while ($running > 0);

        foreach ($chs as $ch) {
            curl_multi_remove_handle($mh, $ch);

            curl_close($ch);
        }
        curl_multi_close($mh);

// результаты
        return ( $callback !== false ) ? true : $results;
    }

    public function actionSetBrandID() {
        while (true) {
            $sql = 'SELECT * FROM "models" WHERE brand_id IS NULL LIMIT 1';
            $model = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->queryRow();
//            print_r($model);exit;
            $sql = 'SELECT * FROM "models" WHERE brand_title = \'' . pg_escape_string($model['brand_title']) . '\' AND brand_id IS NOT NULL';
            $brand = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->queryRow();
//                        print_r($brand);exit;
            if (!empty($brand)) {
                $sql = 'UPDATE "models" SET "brand_id"=\'' . $brand['brand_id'] . '\' WHERE brand_title = \'' . pg_escape_string($model['brand_title']) . '\' AND brand_id IS NULL';
                Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->execute();
            } else {
                $sql = 'UPDATE "models" SET "brand_id"=\'' . $model['row_id'] . '\' WHERE brand_title = \'' . pg_escape_string($model['brand_title']) . '\' AND brand_id IS NULL';
                Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->execute();
            }
        }
    }

    public function actionSetParent() {
        $sql = "select row_id, model_id, upper(brand_title || ' ' || model_test_title) as b_model_title, is_parent from models order by upper(brand_title || ' ' || model_test_title) , row_id limit 1000";
        $models = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->queryAll();
//        print_r($models);exit;
        foreach ($models as $model) {
            $groups[$model['b_model_title']][] = array('id' => (empty($model['is_parent']) ? $model['row_id'] : $model['is_parent']), 'title' => $model['b_model_title'], 'parent' => $model['is_parent']);
        }
        print_r($groups);
    }

    public function actionSql() {
        set_time_limit(0);
        ini_set("memory_limit", '1024M');
//        ini_set("display_errors", false);
        for ($i = 1; $i <= 10; $i++) {
            $sqls = include "sql$i.php";
            foreach ($sqls as $sql) {
                Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->execute();
                echo $sql . "\r\n";
            }
            sleep(10);
        }
    }

    public function actionUrl() {
//        $sql = 'SELECT "pageId" FROM {{pages}} WHERE "pageId" > 18713 AND "pageType" = \'model\'';
//        $model_ids = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->queryAll();
        $model_ids = array(
            24857,
            24325,
            24153,
            22836,
            21331,
            20287,
            20203,
            19998,
            19994,
            19992,
            19990,
            19988,
            19983,
            19982,
            19981,
            19923,
            19922,
            18705,
            13405,
            12837,
            12002,
            12001,
            12000,
            11999,
            11998,
            11996,
            11994,
            11940,
            11918,
            11575,
            8187,
            7495,
            7269,
            3767,
        );
        foreach ($model_ids as $id) {
            $m = Page::model()->findByPk($id);
            if ($m && $m->pageType == 'model') {
                $oldUrl = $m->pageUrl;
                $array = explode('/', $oldUrl);
                $m->pageUrl = $array[0] . '/' . $array[1] . '/' . getUrl::cleanString($m->pageTitle);
                $m->save(false);
                echo $m->pageUrl . "\r\n";
            }
        }
    }

    public function actionTransaction() {
        spl_autoload_unregister(array('YiiBase', 'autoload'));

        $phpExcelPath = Yii::getPathOfAlias('ext.phpexcel');
        include($phpExcelPath . DIRECTORY_SEPARATOR . 'PHPExcel.php');

        spl_autoload_register(array('YiiBase', 'autoload'));

        $file = Yii::app()->basePath . '/../uploads/models.xls';
        $excel = PHPExcel_IOFactory::load($file); // PHPExcel
        $sheets = $excel->getAllSheets();
        foreach ($sheets as $sheet) { // PHPExcel_Worksheet
            $models[] = $sheet->toArray();
        }
        $model = new Models();
        $transaction = Yii::app()->db->beginTransaction();
        try {
            if (!$model->save())
                throw new Exception('Model cannot be saved.');
            $transaction->commit();
        } catch (Exception $e) {
            $transaction->rollback();
        }
    }

    public $models = array(86389,
        86488,
        86627,
        86628,
        86629,
        86467,
        86387,
        86388,
        86390,
        86391,
        86392,
        86393,
        86394,
        86395,
        86397,
        86398,
        86401,
        86402,
        86403,
        86404,
        86405,
        86406,
        86407,
        86411,
        86412,
        86414,
        86415,
        86416,
        86417,
        86419,
        86420,
        86423,
        86425,
        86428,
        86429,
        86430,
        86431,
        86434,
        86435,
        86437,
        86438,
        86439,
        86440,
        86441,
        86442,
        86443,
        86444,
        86445,
        86446,
        86447,
        86448,
        86449,
        86450,
        86452,
        86453,
        86454,
        86457,
        86459,
        86460,
        86461,
        86462,
        86463,
        86465,
        86466,
        86468,
        86469,
        86470,
        86471,
        86472,
        86473,
        86474,
        86475,
        86476,
        86477,
        86478,
        86479,
        86480,
        86481,
        86482,
        86483,
        86484,
        86485,
        86486,
        86487,
        86489,
        86490,
        86491,
        86492,
        86493,
        86494,
        86496,
        86497,
        86498,
        86499,
        86500,
        86501,
        86502,
        86503,
        86504,
        86505,
        86506,
        86507,
        86508,
        86509,
        86510,
        86511,
        86512,
        86513,
        86514,
        86515,
        86516,
        86517,
        86518,
        86519,
        86520,
        86521,
        86522,
        86523,
        86524,
        86525,
        86526,
        86527,
        86528,
        86529,
        86530,
        86531,
        86532,
        86533,
        86534,
        86535,
        86536,
        86537,
        86538,
        86539,
        86540,
        86541,
        86542,
        86543,
        86544,
        86545,
        86546,
        86547,
        86548,
        86549,
        86550,
        86551,
        86552,
        86553,
        86554,
        86555,
        86556,
        86557,
        86558,
        86559,
        86560,
        86561,
        86562,
        86563,
        86564,
        86565,
        86566,
        86567,
        86568,
        86569,
        86570,
        86571,
        86572,
        86573,
        86574,
        86575,
        86576,
        86577,
        86578,
        86579,
        86580,
        86581,
        86582,
        86583,
        86584,
        86585,
        86586,
        86587,
        86588,
        86589,
        86590,
        86591,
        86592,
        86593,
        86594,
        86595,
        86596,
        86597,
        86598,
        86599,
        86600,
        86601,
        86602,
        86603,
        86604,
        86605,
        86606,
        86607,
        86608,
        86609,
        86610,
        86611,
        86612,
        86613,
        86614,
        86615,
        86616,
        86617,
        86618,
        86619,
        86620,
        86621,
        86622,
        86623,
        86624,
        86625,
        86626,
        86495
    );

    public function actionSaveModelsFromExcel($fileName = 'newModels.xls', $idColumn = 'A', $chColumn = 'K') {
        set_time_limit(0);
        ini_set("display_errors", 0);
        error_reporting(0);
        ini_set("memory_limit", '1024M');
        spl_autoload_unregister(array('YiiBase', 'autoload'));
        echo '---' . chr(160) . '---';
        exit;
        $title = "remont-telefonov/samsung/ galaxy-s5-mini-sm-g800h";
        for ($x = 0; $x < strlen($title); $x++) {
            $chs[] = ord($title{$x}) . '=>' . $title{$x};
        }
        echo $title;
        print_r($chs);
        exit;
        $phpExcelPath = Yii::getPathOfAlias('ext.phpexcel');
        include($phpExcelPath . DIRECTORY_SEPARATOR . 'PHPExcel.php');

        spl_autoload_register(array('YiiBase', 'autoload'));

        $file = Yii::app()->basePath . '/../uploads/' . $fileName;
        $excel = PHPExcel_IOFactory::load($file); // PHPExcel
        $sheets = $excel->getAllSheets();
        foreach ($sheets as $sheet) { // PHPExcel_Worksheet
            $models[] = $sheet->toArray();
        }
//       print_r($models[0]);exit;
        foreach ($models[0] as $key => $value) {
            if ((int) $value[0] > 0)
                $ids[] = $value[0];
        }
        print_r($ids);
        exit;
        $this->saveNewModels($this->models);
    }

    protected function saveNewModels(array $ids) {
        $connection = Yii::app()->db;
        $delimiter = chr(160);
        foreach ($ids as $key => $id) {
            $newModel = Yii::app()->db->getCommandBuilder()->createSqlCommand('SELECT cat_id, model_test_title, brand_title, model_yandex_id, model_popular, model_price, model_image FROM models WHERE "row_id" = ' . $id)->queryRow();
            $sql = 'SELECT "pageTitle", "pageId", "pageUrl" FROM {{pages}} WHERE "pageId" = ' . $newModel['cat_id'] . ' AND "pageType" = \'category\';';
            $category = $connection->getCommandBuilder()->createSqlCommand($sql)->queryRow();

            $brandUrl = $category['pageUrl'] . '/' . getUrl::cleanString($newModel['brand_title']);
            $sql = 'SELECT "pageTitle", "pageUrl", "pageId" FROM {{pages}} WHERE upper("pageUrl") = \'' . strtoupper($brandUrl) . '\' AND "pageParent" = \'' . $category['pageId'] . '\' AND "pageType" = \'brand\';';
            $brand = $connection->getCommandBuilder()->createSqlCommand($sql)->queryRow();
            if (!empty($brand)) {
                $parent = $brand['pageId'];
                $brandUrl = $brand['pageUrl'];
                $brandTitle = $brand['pageTitle'];
            } else {
                $data = array(
                    'pageType' => 'brand',
                    'pageAction' => 'list/brand',
                    'pageParent' => $category['pageId'],
                    'pageUrl' => $brandUrl,
                    'pageTitle' => trim($newModel['brand_title'])
                );
                $brandTitle = trim($newModel['brand_title']);
                $connection->getCommandBuilder()->createInsertCommand('{{pages}}', $data)->execute();
                echo 'New brand' . $newModel['brand_title'] . ' => ' . $brandUrl . "\r\n";
                $parent = $connection->getCommandBuilder()->getLastInsertID('{{pages}}');
            }
            $modelTitle = trim($this->replace(str_replace(array($delimiter, chr(194)), '', $newModel['model_test_title'])));

//            for ($x = 0; $x < strlen($modelTitle); $x++) {
//                $chs[] = ord($modelTitle{$x}) . '=>' . $modelTitle{$x};
//            }
//            echo $modelTitle;
//            print_r($chs);
//            echo $modelTitle;
//            exit;
            $modelTitle = iconv(mb_detect_encoding($modelTitle, mb_detect_order(), true), "UTF-8", $modelTitle);
            $modelUrl = $brandUrl . '/' . getUrl::cleanString($modelTitle);
            $image = 'image';
            $sql = 'select "newModel"(' . $parent . '::int8, \'' . $modelUrl . '\'::varchar, \'' . pg_escape_string($modelTitle) . '\'::varchar, \'' . pg_escape_string($image) . '\'::varchar, ' . time() . '::int8, \'' . pg_escape_string($newModel['model_yandex_id']) . '\'::varchar,  ' . $newModel['model_popular'] . '::int2, ' . $newModel['model_price'] . '::int8, \'' . $newModel['model_image'] . '\'::varchar);';

            $connection->getCommandBuilder()->createSqlCommand($sql)->execute();
            file_put_contents(Yii::app()->basePath . '/../uploads/sql.sql', $sql . "\r\n", FILE_APPEND);
            echo $key . ' => ' . $modelUrl . "\r\n";
        }
    }

    public function actionGetNewModels() {
//        echo date('y-m-d H:i:s', 1379894400);exit;
        $delimiter = chr(194);
        set_time_limit(0);
        Yii::import('ext.phpQuery.phpQuery');
        Yii::import('application.models.Page');
        foreach ($this->categoryUrls as $cTitle => $data) {
            $products = array();
            $out = Yii::app()->curl->get($data['url']);
//             $out = file_get_contents($data['url']);
            echo $out;
//        exit;
            $doc = phpQuery::newDocument($out);
            $ids = array();
            $category = trim(strip_tags(pq($doc['.b-breadcrumbs a:nth-child(3):first'])->text()));
            $brand = trim(strip_tags(pq($doc['.b-breadcrumbs b:first'])->text()));
            $criteria = new CDbCriteria();
            $criteria->select = '"pageId", "pageTitle"';
            $criteria->compare('"pageType"', 'model');
            foreach ($doc['.page__b-offers__guru .b-offers'] as $popular => $id) {
                preg_match('@(.*)modelid=(\d+)(.*)@', pq($id)->find('.b-offers__pict a')->attr('href'), $content);
                if (isset($content[2])) {
                    $criteria->compare('"pageYandexId"', (int) $content[2], true);
                    $model = Page::model()->find($criteria);
                    if ($model !== null) {
                        $products[] = array(
                            'status' => 'yes',
                            'product' => (array) $model
                        );
                    } else {
                        $title = trim(strip_tags(str_replace('новинка', '', pq($id)->find('.b-offers__desc .b-offers__title a')->html())));
//                        for($x=0;$x<strlen($title);$x++){ 
//                            echo ord($title[$x]) . ', ';
//                        }
//                        echo $title;
//                        exit;
                        $nameArr = explode($delimiter, $title);

                        if (count($nameArr) < 2) {
                            $nameArr = explode(chr(32), $title);
                        }
                        $image = str_replace('&size=2', '', pq($id)->find('.b-offers__pict a img')->attr('src'));
                        $price = pq($id)->find('.b-offers__info .b-offers__price .b-prices .b-prices__num')->text();
                        $price = preg_replace("/\D/", '', $price);
                        list($brand_title, $model_title) = $nameArr;
                        echo $model_title;
                        $title_t = $model_title;
                        $bTitle = iconv(mb_detect_encoding($brand_title, mb_detect_order(), true), "UTF-8", $brand_title);
                        $mTitle = iconv(mb_detect_encoding($title, mb_detect_order(), true), "UTF-8", $title);
                        $product = array(
                            'model_yandex_id' => (int) $content[2],
                            'model_title' => $mTitle,
                            'cat_id' => $data['id'],
                            'cat_title' => iconv(mb_detect_encoding($cTitle, mb_detect_order(), true), "UTF-8", $cTitle),
                            'brand_title' => $bTitle,
                            'model_test_title' => $this->replace(str_replace($bTitle, '', $mTitle)),
                            'model_image' => $image,
                            'model_price' => (int) $price,
                            'model_popular' => $popular,
                            'model_status' => 'new',
                            'model_added_date' => time(),
                        );
                        $products[] = array(
                            'status' => 'no',
                            'product' => $product
                        );
                    }
                }
            }
            foreach ($products as $product) {
                if ($product['status'] == 'no') {
                    $model = Yii::app()->db->getCommandBuilder()->createSqlCommand('SELECT model_test_title FROM models WHERE model_yandex_id = ' . $product['product']['model_yandex_id'])->queryAll();
                    if (empty($model)) {
                        print_r($product);
//                        $sql = 'INSERT INTO models (model_yandex_id, model_title, category_title, brand_title, model_test_title, model_status) VALUES (\''.$product['product']['model_yandex_id'].'\', \''.$product['product']['model_title'].'\', \''.$product['product']['category_title'].'\', \''.$product['product']['brand_title'].'\', \''.$product['product']['model_test_title'].'\' ,\'new\')';
//                        Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->execute();
                        Yii::app()->db->getCommandBuilder()->createInsertCommand('models', $product['product'])->execute();
                    }
                }
            }
//            print_r($products);
        }
    }

    public function actionGetTopModels() {
        $delimiter = chr(194);
        set_time_limit(0);
        Yii::import('ext.phpQuery.phpQuery');
        Yii::import('application.models.Page');
        foreach ($this->categoryTopUrls as $cTitle => $data) {
            $products = array();
            $out = Yii::app()->curl->get($data['url']);
            echo $out;
            $doc = phpQuery::newDocument($out);
            $ids = array();
            $category = trim(strip_tags(pq($doc['.b-breadcrumbs a:nth-child(3):first'])->text()));
            $brand = trim(strip_tags(pq($doc['.b-breadcrumbs b:first'])->text()));
            $criteria = new CDbCriteria();
            $criteria->select = '"pageId", "pageTitle"';
            $criteria->compare('"pageType"', 'model');
            foreach ($doc['.page__b-offers__guru .b-offers'] as $popular => $id) {
                preg_match('@(.*)modelid=(\d+)(.*)@', pq($id)->find('.b-offers__pict a')->attr('href'), $content);
                if (isset($content[2])) {
                    $title = trim(strip_tags(str_replace('новинка', '', pq($id)->find('.b-offers__desc .b-offers__title a')->html())));
                    $nameArr = explode($delimiter, $title);
                    if (count($nameArr) < 2) {
                        $nameArr = explode(chr(32), $title);
                    }
                    $image = str_replace('&size=2', '', pq($id)->find('.b-offers__pict a img')->attr('src'));
                    $price = pq($id)->find('.b-offers__info .b-offers__price .b-prices .b-prices__num')->text();
                    $price = preg_replace("/\D/", '', $price);
                    list($brand_title, $model_title) = $nameArr;
                    echo $model_title;
                    $title_t = $model_title;
                    $product = array(
                        'model_yandex_id' => (int) $content[2],
                        'model_title' => iconv(mb_detect_encoding($title, mb_detect_order(), true), "UTF-8", $title),
                        'cat_id' => $data['id'],
                        'cat_title' => iconv(mb_detect_encoding($cTitle, mb_detect_order(), true), "UTF-8", $cTitle),
                        'brand_title' => iconv(mb_detect_encoding($brand_title, mb_detect_order(), true), "UTF-8", $brand_title),
                        'model_test_title' => mb_substr($title_t, 1, strlen($title_t), 'utf-8'),
                        'model_image' => $image,
                        'model_price' => (int) $price,
                        'model_popular' => $popular,
                        'model_status' => 'new',
                        'model_added_date' => time(),
                    );
                    $products[] = array(
                        'status' => 'no',
                        'product' => $product
                    );
                }
            }
            foreach ($products as $product) {
                if ($product['status'] == 'no') {
                    print_r($product);
                    Yii::app()->db->getCommandBuilder()->createInsertCommand('top_models', $product['product'])->execute();
                }
            }
//            print_r($products);
        }
    }

    public function actionCopyBrandImages() {
        set_time_limit(0);
        ini_set("memory_limit", '1024M');
        ini_set("display_errors", false);
        error_reporting(false);
        $folder = Yii::app()->basePath . '/../uploads/brand_images';
        $files = scandir($folder);
        unset($files[0]);
        unset($files[1]);
        print_r($files);
        foreach ($files as $key => $file) {
            //echo pathinfo($folder . '/' . $file, PATHINFO_FILENAME);exit;
            $brand = Page::model()->find('"pageUrl" LIKE \'%' . pathinfo($folder . '/' . $file, PATHINFO_FILENAME) . '%\' AND "pageType" = \'brand\' AND "pageId">25465');
            //print_r($brand);exit;
            if (!empty($brand)) {
                $image = str_replace('/', '-', $brand['pageUrl']) . '.' . pathinfo($folder . '/' . $file, PATHINFO_EXTENSION);
                copy($folder . '/' . $file, Yii::app()->basePath . '/../uploads/brands/' . $image);
                $brand->pageImages = $image;
                $brand->save(false);
            }
        }
    }

    public function actionUpdateBrandImage() {
        $fromId = 18713;
        $sql = 'SELECT "pageTitle", "pageLinkedServices", "pageUrl", "pageId" FROM {{pages}} WHERE "pageId" > ' . $fromId . ' AND "pageType" = \'brand\';';
        $models = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->queryAll();
        foreach ($models as $model) {
            $m = Page::model()->findByPk($model['pageId']);
            $m->pageImages = getUrl::cleanString($model['pageTitle']) . '.jpg';
            $m->save(false);
        }
    }

    public function actionGetModelImageFromYandex() {
//        echo 'sdf';exit;
        $fromId = 24997;
        $sql = 'SELECT "pageTitle", "pageLinkedServices", "pageUrl", "pageId" FROM {{pages}} WHERE "pageImages" = \'image\' AND "pageType" = \'model\';';
        $models = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->queryAll();
        print_r($models);
        foreach ($models as $model) {
            if (filter_var($model['pageLinkedServices'], FILTER_VALIDATE_URL)) {
                if (getimagesize($model['pageLinkedServices'])) {
                    $image = str_replace('/', '-', $model['pageUrl']) . '.' . pathinfo($model['pageLinkedServices'], PATHINFO_EXTENSION);
                    copy($model['pageLinkedServices'], Yii::app()->basePath . '/../uploads/models/' . $image);
                    $m = Page::model()->findByPk($model['pageId']);
                    $m->pageImages = $image;
                    $m->save(false);
                }
            }
        }
    }

    public function actionSetModelRussianName() {
        set_time_limit(0);
        ini_set("memory_limit", '1024M');
        ini_set("display_errors", false);
        error_reporting(false);
        spl_autoload_unregister(array('YiiBase', 'autoload'));

        $phpExcelPath = Yii::getPathOfAlias('ext.phpexcel');
        include($phpExcelPath . DIRECTORY_SEPARATOR . 'PHPExcel.php');

        spl_autoload_register(array('YiiBase', 'autoload'));

        $file = Yii::app()->basePath . '/../uploads/russian_name.xls';
        $excel = PHPExcel_IOFactory::load($file); // PHPExcel
        $sheets = $excel->getAllSheets();
        foreach ($sheets as $sheet) { // PHPExcel_Worksheet
            $models[] = $sheet->toArray();
        }

        unset($models[0][0]);
        foreach ($models[0] as $key => $model) {
            $sql = 'SELECT "pageRussianName", "itemId" FROM {{metas}} WHERE "itemId" = ' . $model[0] . ' AND "itemType" = \'page\';';
            $meta = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->queryRow();
            if (!empty($meta)) {
                echo $sql = 'UPDATE "mb_metas" SET "pageRussianName" = \'' . $model[3] . '\' WHERE "itemId" = ' . $model[0] . ' AND "itemType" = \'page\';';
                Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->execute();
            } else {
                echo $sql = 'INSERT INTO "mb_metas" ("itemType", "itemId", "pageRussianName") VALUES (\'page\', ' . $model[0] . ', \'' . $model[3] . '\');';
                Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->execute();
            }
        }
    }

    public function actionAddModels() {
        set_time_limit(0);
        ini_set("memory_limit", '1024M');
        ini_set("display_errors", false);
        error_reporting(false);
        spl_autoload_unregister(array('YiiBase', 'autoload'));

        $phpExcelPath = Yii::getPathOfAlias('ext.phpexcel');
        include($phpExcelPath . DIRECTORY_SEPARATOR . 'PHPExcel.php');

        spl_autoload_register(array('YiiBase', 'autoload'));

        $file = Yii::app()->basePath . '/../uploads/models.xls';
        $excel = PHPExcel_IOFactory::load($file); // PHPExcel
        $sheets = $excel->getAllSheets();
        foreach ($sheets as $sheet) { // PHPExcel_Worksheet
            $models[] = $sheet->toArray();
        }

        unset($models[0][0]);
        $replaces = array('iPhone' => 'apple', 'Apple iPad' => 'apple', 'Macbook' => 'apple', 'Apple iPod' => 'apple');

        foreach ($models[0] as $key => $model) {

            $sql = 'SELECT "pageTitle", "pageId", "pageUrl" FROM {{pages}} WHERE "pageId" = ' . $model[1] . ' AND "pageType" = \'category\';';
            $category = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->queryRow();

            $brandUrl = $category['pageUrl'] . '/' . getUrl::cleanString($model[4]);
            $sql = 'SELECT "pageTitle", "pageUrl", "pageId" FROM {{pages}} WHERE upper("pageUrl") = \'' . strtoupper($brandUrl) . '\' AND "pageParent" = \'' . $category['pageId'] . '\' AND "pageType" = \'brand\';';
            $brand = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->queryRow();
            if (!empty($brand)) {
                $parent = $brand['pageId'];
                $brandUrl = $brand['pageUrl'];
            } else {

                $data = array(
                    'pageType' => 'brand',
                    'pageAction' => 'list/brand',
                    'pageParent' => $category['pageId'],
                    'pageUrl' => $brandUrl,
                    'pageTitle' => $model[4]
                );
                Yii::app()->db->getCommandBuilder()->createInsertCommand('{{pages}}', $data)->execute();
                $parent = Yii::app()->db->getCommandBuilder()->getLastInsertID('{{pages}}');
                $array[$model[3]] = $parent;
            }
            $modelUrl = $brandUrl . '/' . getUrl::cleanString($model[15]);
            $image = 'image';
//            print_r($model[7]);exit;
//            if (filter_var($model[7], FILTER_VALIDATE_URL)) {
//                if (getimagesize($model[7])) {
//                    $image = str_replace('/', '-', $modelUrl) . '.' . pathinfo($model[7], PATHINFO_EXTENSION);
//                    copy($model[7], Yii::app()->basePath . '/../uploads/models/' . $image);
//                }
//            }
//            $sql = 'SELECT "pageId" FROM {{pages}} WHERE "pageUrl" = LOWER(\'' . strtolower($modelUrl) . '\') AND "pageType" = \'model\';';
//            $modelTitle = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->queryRow();
//            if (!empty($modelTitle))
//                $modelUrl .= '-new';
            $sql = 'select "newModel"(' . $parent . '::int8, \'' . $modelUrl . '\'::varchar, \'' . pg_escape_string($model[15]) . '\'::varchar, \'' . pg_escape_string($image) . '\'::varchar, ' . time() . '::int8, \'' . pg_escape_string($model[11]) . '\'::varchar,  ' . $model[10] . '::int2, ' . $model[9] . '::int8, \'' . pg_escape_string($model[7]) . '\'::varchar);';
//            $data = array(
//                'pageParent' => $parent,
//                'pageType' => 'model',
//                'pageActive' => 1,
//                'pageAction' => 'list/model',
//                'pageUrl' => $modelUrl,
//                'pageTitle' => pg_escape_string($model[15]),
//                'pageImages' => $image,
//                'pageCreateDate' => time(),
//                'pageYandexId' => $model[11],
//                'pageYandexPopular' => $model[10],
//                'pageYandexPrice' => $model[9],
//                'pageLinkedServices' => $model[7],
//            );
//            Yii::app()->db->getCommandBuilder()->createInsertCommand('{{pages}}', $data)->execute();
            Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->execute();
            echo $key . "\r\n";
            print_r($sql);
        }
    }

    public function actionBack() {
        $sql = "SELECT row_id, brand_title, model_new_title FROM models where model_new_title is not null";
        $rows = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->queryAll();
        foreach ($rows as $row) {
            $title = str_replace('\&quot;', '', $row['model_new_title']);
            $sql = "update models SET model_new_title='{$title}' WHERE row_id={$row['row_id']};";
            Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->execute();
            echo date('d-m-Y H:i:s') . ' - ' . ": => " . $sql . "\r\n";
        }
    }

    public function actionRemoveBrand() {
        $sql = "SELECT row_id, brand_title, model_new_title FROM models where model_new_title is not null";
        $rows = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->queryAll();
        foreach ($rows as $row) {
            $title = str_replace($row['brand_title'], '', $row['model_new_title']);
            $title = CHtml::encode($title);
            $sql = "update models SET model_new_title='{$title}' WHERE row_id={$row['row_id']};";
            Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->execute();
            echo date('d-m-Y H:i:s') . ' - ' . ": => " . $sql . "\r\n";
        }
    }

    public function actionParent() {
//        set_time_limit(0);
//        ini_set("memory_limit", '1024M');
        $sql = "SELECT row_id, model_new_title FROM models where is_active =1 order by cat_id, brand_id, model_new_title";
        $rows = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->queryAll();
        foreach ($rows as $row) {
            if (!isset($old) || strtoupper($old['model_new_title']) != strtoupper($row['model_new_title'])) {
                Yii::app()->db->getCommandBuilder()->createSqlCommand("update models SET is_parent='parent' WHERE row_id={$row['row_id']};")->execute();
            } else if (isset($old) && strtoupper($old['model_new_title']) == strtoupper($row['model_new_title'])) {
                Yii::app()->db->getCommandBuilder()->createSqlCommand("update models SET is_parent='{$old['row_id']}' WHERE row_id={$row['row_id']};")->execute();
            }
            $old = $row;
            print_r($old);
        }
    }

    protected function replace($str) {
        $value = array(' 16Gb', ' 32Gb',
            ' 64Gb', ' 8Gb', ' 4Gb',
            ' 128Gb', ' 2Gb', ' 8Gb',
            ' 160Gb', ' 4Gb', ' 1Gb',
            ' 64Gb', ' 256Gb', ' 512Mb',
            ' 256Mb', ' 128Mb', ' 0Gb',
            ' RAM', ' Dual Sim', ' DG',
            ' 2Gb', ' Luxury Edition',
            ' Yandex', ' 2Gb Ram',
            ' 768Mb Ram',
            ' 1Gb Ram',
            ' DDR3', ' eMMC', ' 3G', ' SSD', ' DOS', ' DDR2', ' RAM',
            ' i5', ' i3', ' dock', ' keyboard',
            ' 4G', '+', ' Wi-Fi', 'Cellular', ' dock', 'i7',
            ' keyboard', ' LTE', ' 3G', ' LTE', ' Win7',
            ' HP', ' IntelAtom', ' Z690', ' n2600',
            'AMD', 'Z-60', 'Core', 'Intel',
            ' 1.5GHz', ' Black', ' White',
            ' 253', ' iRu', ' eMMC', ' SSD',
            ' DOS', ' DDR2', ' RAM',
            ' ROM', ' 279', 'Microsoft',
            ' Touch Cover', '+ Yota', ' Android 4.0',
            ' Android 3.2', ' 32 HDD',
            ' 50 HDD', ' 75 HDD', '\'', '"'
        );
        return trim(str_replace($value, '', $str));
    }

    public function actionRep() {
        $value = array(' 16Gb', ' 32Gb',
            ' 64Gb', ' 8Gb', ' 4Gb',
            ' 128Gb', ' 2Gb', ' 8Gb',
            ' 160Gb', ' 4Gb', ' 1Gb',
            ' 64Gb', ' 256Gb', ' 512Mb',
            ' 256Mb', ' 128Mb', ' 0Gb',
            ' RAM', ' Dual Sim', ' DG',
            ' 2Gb', ' Luxury Edition',
            ' Yandex', ' 2Gb Ram',
            ' 768Mb Ram',
            ' 1Gb Ram',
            ' DDR3', ' eMMC', ' 3G', ' SSD', ' DOS', ' DDR2', ' RAM',
            ' i5', ' i3', ' dock', ' keyboard',
            ' 4G', '+', ' Wi-Fi', 'Cellular', ' dock', 'i7',
            ' keyboard', ' LTE', ' 3G', ' LTE', ' Win7',
            ' HP', ' IntelAtom', ' Z690', ' n2600',
            'AMD', 'Z-60', 'Core', 'Intel',
            ' 1.5GHz', ' Black', ' White',
            ' 253', ' iRu', ' eMMC', ' SSD',
            ' DOS', ' DDR2', ' RAM',
            ' ROM', ' 279', 'Microsoft',
            ' Touch Cover', '+ Yota', ' Android 4.0',
            ' Android 3.2', ' 32 HDD',
            ' 50 HDD', ' 75 HDD', '\'', '"'
        );

        $sql = 'SELECT model_test_title, row_id FROM models where row_id > 86386';
        $models = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->queryAll();
//        print_r($models);exit;
        foreach ($models as $model) {
            $title = trim(str_replace($value, '', $model['model_test_title']));
            $sql = 'UPDATE top_models SET model_test_title = \'' . $title . '\' WHERE row_id = ' . $model['row_id'];
            echo date('d-m-Y H:i:s') . ' - ' . ": => " . $sql . "\r\n";
            Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->execute();
        }
    }

    public function actionDisabled() {
        $disableds = array(157, 160, 163, 164, 165, 168, 169, 171, 176, 180, 183, 184, 185, 187, 193, 189, 194, 195, 190, 197, 198, 199, 200, 202, 203, 204, 210, 211, 212, 215, 216, 217, 219, 221, 222, 226, 230, 225, 234, 249, 248, 238, 250, 251, 245, 239, 240, 247, 252, 254, 255, 256, 257, 258, 260, 261, 262, 267, 269, 265, 270, 273, 274, 275, 271, 276, 277, 278, 280, 281, 283, 284, 286, 288, 289, 290, 292, 293, 294, 295, 297, 298, 296, 300, 301, 302, 305, 309, 310, 312, 318, 319, 320, 321, 322, 323, 336, 335, 331, 339, 340, 332, 342, 343, 344, 347, 348, 350, 352, 353, 355, 346, 358, 359, 362, 363, 366, 367);
        $sql = 'UPDATE models SET is_active = 0 WHERE brand_id IN (' . implode(',', $disableds) . ')';
        echo date('d-m-Y H:i:s') . ' - ' . ": => " . $sql . "\r\n";
        Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->execute();
    }

    public function actionReplace() {

        $replaces[11] = array('16Gb', '32Gb', '64Gb', '8Gb');
        $replaces[4] = array('16Gb', '32Gb', '64Gb', '8Gb');
        $replaces[57] = array('768Mb Ram', '1Gb Ram');
        $replaces[55] = array('Dual Sim', '16Gb', '32Gb');
        $replaces[58] = array('Yandex', '2Gb Ram');
        $replaces[71] = array('16Gb', '32Gb', '64Gb', '8Gb', '4Gb');
        $replaces[70] = array('16Gb', '32Gb', '64Gb', '8Gb', '4Gb');
        $replaces[77] = array('16Gb', '32Gb', '64Gb', '8Gb', '4Gb', '128Gb');
        $replaces[80] = array('16Gb', '32Gb', '64Gb', '8Gb', '4Gb', '128Gb', 'Dual Sim', 'DG', '2Gb', 'Luxury Edition');
        $replaces[84] = array('16Gb', '32Gb', '64Gb', '8Gb', '4Gb', '128Gb', 'Dual Sim');
        $replaces[86] = array('16Gb', '32Gb', '64Gb', '8Gb', '4Gb', '128Gb');
        $replaces[111] = array('16Gb', '32Gb', '64Gb', '8Gb', '4Gb', '128Gb');
        $replaces[147] = array('16Gb', '32Gb', '64Gb', '8Gb', '4Gb', '128Gb');
        $replaces[153] = array('16Gb', '32Gb', '64Gb', '8Gb', '4Gb', '128Gb', 'RAM');
        $disableds = '';

        foreach ($replaces as $key => $value) {
//          $sql = 'UPDATE models SET title = \'' . $title . '\' WHERE id = ' . $key;
            $sql = 'SELECT model_title, id FROM models WHERE brand_id = \'' . $key . '\'';
            $models = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->queryAll();
            foreach ($models as $model) {
                $title = trim(str_replace($value, '', $model['model_title']));
                $sql = 'UPDATE models SET title = \'' . $title . '\' WHERE id = ' . $model['id'];
                echo date('d-m-Y H:i:s') . ' - ' . ": => " . $sql . "\r\n";
                Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->execute();
            }
        }
    }

    public function actionGet() {
        set_time_limit(0);
        ini_set("memory_limit", '1024M');
        Yii::import('ext.phpQuery.phpQuery');
        $random = array_rand($this->urls);
//        print_r($random);exit;
        $a = 0;
        while (true) {
            $a++;
            $sql = 'select * from mb_model2yandex where "yandexTitle" is null LIMIT 1';
            $model = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->queryAll();
            if (!$model)
                exit;
            $sql = 'UPDATE mb_model2yandex SET "yandexTitle"=\'\' WHERE "modelId" = ' . $model[0]['modelId'] . ' AND "yandexId" = ' . $model[0]['yandexId'];
            $c = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->execute();

            $url = 'http://market.yandex.ru/model.xml?modelid=' . $model[0]['yandexId'];
            $output = Yii::app()->YandexData->get($url);
            $doc = phpQuery::newDocument($output);
            $name = trim(strip_tags(str_replace('новинка', '', pq($doc['h1.b-page-title__title'])->html())));
//            sleep(rand(0, 3));
//            Yii::app()->YandexData->get($this->urls[$random]);
            if (!empty($name) && strlen($name) > 4) {
                $sql = 'UPDATE mb_model2yandex SET "yandexTitle"=\'' . CHtml::encode($name) . '\' WHERE "modelId" = ' . $model[0]['modelId'] . ' AND "yandexId" = ' . $model[0]['yandexId'];
                $c = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->execute();
//                 print_r($c);exit;
                echo $a . ' - ' . date('d-m-Y H:i:s') . ' - ' . ": => " . $sql . "\r\n";
            }
//            sleep(rand(0, 2));
//            Yii::app()->YandexData->get($this->urls[$random]);
        }
    }

    public function actionGetModels() {
        $catUrl = 'http://market.yandex.ru/catalogmodels.xml?CAT_ID=160043&hid=91491';
        set_time_limit(0);
        Yii::import('ext.phpQuery.phpQuery');
        $output = Yii::app()->YandexData->get($catUrl);
        $document = phpQuery::newDocument($output);
        $allBrandsUrl = pq($document['.b-category-pop-vendors a.black'])->attr('href');
        $output = Yii::app()->YandexData->get('http://market.yandex.ru' . $allBrandsUrl);
        $document = phpQuery::newDocument($output);
        foreach ($document['.b-vendor__item'] as $urls) {
            $brandUrls[] = pq($urls)->find('a.b-link')->attr('href');
        }
        print_r($brandUrls);
//        exit;
        foreach ($brandUrls as $k => $url) {
//            Yii::app()->session['brandUrl'] = 
            $file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'tmp.txt';
            $prepared = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'prepared.txt';
            $urls_ = file_get_contents($prepared, true);
            $aUrls = json_decode($urls_, true);
            print_r($aUrls);
            if (!isset($aUrls[$url])) {
                $fp = fopen($file, 'w');
                $output = Yii::app()->YandexData->get('http://market.yandex.ru' . $url);
                fwrite($fp, $output);
                $document = phpQuery::newDocument($output);
                $pager = pq($document['a.b-pager__next'])->attr('href');
                if (!empty($pager)) {
                    $nextPage = '';
                    $flag = false;
                    $nextPage = pq($document['a.b-pager__next'])->attr('href');
                    while (!$flag) {
                        sleep(1);
                        $out = Yii::app()->YandexData->get('http://market.yandex.ru/' . $nextPage);
                        $d = phpQuery::newDocument($out);
                        $nextPage = pq($d['.b-pager span .b-pager__next'])->attr('href');
                        if (empty($nextPage))
                            $flag = true;
                        fwrite($fp, $out);
                    }
                }
                fclose($fp);
                $content = file_get_contents($file, true);
                $doc = phpQuery::newDocument($content);
                $category = trim(strip_tags(pq($doc['.b-breadcrumbs span a span'])->text()));
                $brand = trim(strip_tags(pq($doc['.b-breadcrumbs strong'])->text()));
                echo 'content=>' . $category . '-' . $brand . "\r\n";
                if (!isset($products))
                    $products = array();
                foreach ($doc['.page__b-offers__guru .b-offers'] as $key => $id) {
                    echo 'Product=>' . $key . "\r\n";
                    preg_match('@(.*)modelid=(\d+)(.*)@', pq($id)->find('.b-offers__pict a')->attr('href'), $content);
                    if (isset($content[2])) {
                        $featuresUrl = 'http://market.yandex.ru/compare.xml?hid=91491&CAT_ID=160043&CMD=-CMP=' . $content[2];
                        $output = Yii::app()->YandexData->get($featuresUrl);
                        $document = phpQuery::newDocument($output);
                        $features = array();
                        foreach ($document['.l-compare__body tr'] as $fs) {
                            $features[] = array('name' => strip_tags(pq($fs)->find('td.l-compare__name')->text()), 'value' => strip_tags(pq($fs)->find('td.l-compare__model')->text()));
                        }
                        $h1 = trim(pq($id)->find('.b-offers__desc .b-offers__title a')->text());
                        $products[] = array(
                            'mainImageUrl' => str_replace('&size=2', '', pq($id)->find('.b-offers__pict a img')->attr('src')),
                            'h1Title' => $h1,
                            'category' => $category,
                            'brand' => $brand,
                            'model' => mb_substr(str_replace($brand, '', $h1), 1, strlen($h1), 'utf-8'),
                            'yaId' => $content[2],
                            'yaPrice' => str_replace(" ", '', pq($id)->find('.b-prices__num')->text()),
                            'yaRating' => $key,
                            'features' => $features
                        );
                    }
                }

                $f = fopen($prepared, 'w');
                fwrite($f, $urls_);
                fwrite($f, json_encode(array($url => $k)));
                fclose($f);
            }
        }
        $models = json_encode($products);
        $file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'models.txt';
        $fp = fopen($file, 'w');
        fwrite($fp, $models);
        fclose($fp);
        echo 'Finish';
//        print_r($products);
    }

    public function actionGetModels2() {
        $catUrl = 'http://market.yandex.ru/catalogmodels.xml?CAT_ID=160043&hid=91491';
        set_time_limit(0);
        Yii::import('ext.phpQuery.phpQuery');
        $output = Yii::app()->YandexData->get($catUrl);
        $document = phpQuery::newDocument($output);
        $allBrandsUrl = pq($document['.b-category-pop-vendors a.black'])->attr('href');
        $output = Yii::app()->YandexData->get('http://market.yandex.ru' . $allBrandsUrl);
        $document = phpQuery::newDocument($output);
        foreach ($document['.b-vendor__item'] as $urls) {
            $brandUrls[] = pq($urls)->find('a.b-link')->attr('href');
        }
        print_r($brandUrls);
//        exit;
        foreach ($brandUrls as $k => $url) {
//            Yii::app()->session['brandUrl'] = 
            $file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'tmp.txt';
            $prepared = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'prepared.txt';
            $urls_ = file_get_contents($prepared, true);
            $aUrls = json_decode($urls_, true);
            print_r($aUrls);
            if (!isset($aUrls[$url])) {
                $fp = fopen($file, 'w');
                $output = Yii::app()->YandexData->get('http://market.yandex.ru' . $url);
                fwrite($fp, $output);
                $document = phpQuery::newDocument($output);
                $pager = pq($document['a.b-pager__next'])->attr('href');
                if (!empty($pager)) {
                    $nextPage = '';
                    $flag = false;
                    $nextPage = pq($document['a.b-pager__next'])->attr('href');
                    while (!$flag) {
                        sleep(1);
                        $out = Yii::app()->YandexData->get('http://market.yandex.ru/' . $nextPage);
                        $d = phpQuery::newDocument($out);
                        $nextPage = pq($d['.b-pager span .b-pager__next'])->attr('href');
                        if (empty($nextPage))
                            $flag = true;
                        fwrite($fp, $out);
                    }
                }
                fclose($fp);
                $content = file_get_contents($file, true);
                $doc = phpQuery::newDocument($content);
                $category = trim(strip_tags(pq($doc['.b-breadcrumbs span a span'])->text()));
                $brand = trim(strip_tags(pq($doc['.b-breadcrumbs strong'])->text()));
                echo 'content=>' . $category . '-' . $brand . "\r\n";
                if (!isset($products))
                    $products = array();
                foreach ($doc['.page__b-offers__guru .b-offers'] as $key => $id) {
                    echo 'Product=>' . $key . "\r\n";
                    preg_match('@(.*)modelid=(\d+)(.*)@', pq($id)->find('.b-offers__pict a')->attr('href'), $content);
                    if (isset($content[2])) {
                        $featuresUrl = 'http://market.yandex.ru/compare.xml?hid=91491&CAT_ID=160043&CMD=-CMP=' . $content[2];
                        $output = Yii::app()->YandexData->get($featuresUrl);
                        $document = phpQuery::newDocument($output);
                        $features = array();
                        foreach ($document['.l-compare__body tr'] as $fs) {
                            $features[] = array('name' => strip_tags(pq($fs)->find('td.l-compare__name')->text()), 'value' => strip_tags(pq($fs)->find('td.l-compare__model')->text()));
                        }
                        $h1 = trim(pq($id)->find('.b-offers__desc .b-offers__title a')->text());
                        $products[] = array(
                            'mainImageUrl' => str_replace('&size=2', '', pq($id)->find('.b-offers__pict a img')->attr('src')),
                            'h1Title' => $h1,
                            'category' => $category,
                            'brand' => $brand,
                            'model' => mb_substr(str_replace($brand, '', $h1), 1, strlen($h1), 'utf-8'),
                            'yaId' => $content[2],
                            'yaPrice' => str_replace(" ", '', pq($id)->find('.b-prices__num')->text()),
                            'yaRating' => $key,
                            'features' => $features
                        );
                    }
                }

                $f = fopen($prepared, 'w');
                fwrite($f, $urls_);
                fwrite($f, json_encode(array($url => $k)));
                fclose($f);
            }
        }
        $models = json_encode($products);
        $file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'models.txt';
        $fp = fopen($file, 'w');
        fwrite($fp, $models);
        fclose($fp);
        echo 'Finish';
//        print_r($products);
    }

    public function actionList() {
        $declaredClasses = get_declared_classes();
        foreach (glob(Yii::getPathOfAlias('application.controllers') . "/*Controller.php") as $controller) {
            $class = basename($controller, ".php");
            if (!in_array($class, $declaredClasses))
                Yii::import("application.controllers." . $class, true);
            $reflection = new ReflectionClass($class);
            $methods = $reflection->getMethods();
            $methods = get_class_methods($class);
            foreach ($methods as $method) {
                if (strpos($method, 'action') !== false && $method != 'actions') {
                    $actions[] = lcfirst(str_replace('Controller', '', $class)) . '/' . lcfirst(str_replace('action', '', $method));
                }
            }
        }
        print_r($actions);
    }

}
